package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.enums.ConferenceStateEnum;
import com.cadre.common.utils.R;
import com.cadre.system.domain.officer.BatchconferenceEntity;
import com.cadre.system.domain.officer.ConfernamelistEntity;
import com.cadre.system.domain.officer.PostchangeEntity;
import com.cadre.system.service.BatchconferenceService;
import com.cadre.system.service.ConfernamelistService;
import com.cadre.system.service.PostchangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@Api(tags = {"上会批次接口"})
@RequestMapping({"/profess/batchconference"})
@RestController
/* loaded from: BatchconferenceController.class */
public class BatchconferenceController {
    @Autowired
    private BatchconferenceService batchconferenceService;
    @Autowired
    private ConfernamelistService confernamelistService;
    @Autowired
    private PostchangeService postchangeService;

    @PostMapping({"/list"})
    @ApiOperation("查询上会批次")
    public R list(@RequestParam Map<String, Object> stata) {
        return R.ok().put("page", (Object) this.batchconferenceService.queryPage(stata));
    }

    @PostMapping({"/listForHistory"})
    @ApiOperation("查询历史上会批次")
    public R listForHistory(@RequestParam Map<String, Object> stata) {
        return R.ok().put("page", (Object) this.batchconferenceService.queryPageForHistory(stata));
    }

    @RequestMapping({"/info/{flowid}"})
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("batchconference", (Object) ((BatchconferenceEntity) this.batchconferenceService.getBaseMapper().selectById(flowid)));
    }

    @PostMapping({"/save"})
    @ApiOperation("新增上会批次")
    public R save(@RequestBody BatchconferenceEntity batchconference) {
        batchconference.setCreatetime(new Date());
        batchconference.setStata(ConferenceStateEnum.NOT_SUMMARIZED.getValue());
        this.batchconferenceService.getBaseMapper().insert(batchconference);
        return R.ok();
    }

    @PostMapping({"/toBeAttended"})
    @ApiOperation("待上会列表")
    public R toAttendedlist() {
        return R.ok().put("data", (Object) this.batchconferenceService.getBaseMapper().selectList(new LambdaQueryWrapper<BatchconferenceEntity>().eq(BatchconferenceEntity::getStata, ConferenceStateEnum.TO_BE_ATTENDED.getValue())));
    }

    @PostMapping({"/notSummarized"})
    @ApiOperation("未汇总批次")
    public R notSummarized() {
        //（已调整完毕） todo:将sql迁移到xml文件中
        return R.ok().put("data", (Object) this.batchconferenceService.getBaseMapper().selectList(
                new LambdaQueryWrapper<BatchconferenceEntity>().select(BatchconferenceEntity::getFlowid,BatchconferenceEntity::getName).
                        eq(BatchconferenceEntity::getStata, ConferenceStateEnum.NOT_SUMMARIZED.getValue())));
    }

    @GetMapping({"/summarzid"})
    @ApiOperation("汇总批次")
    public R summarzid(String flowid) {
        System.out.println(">>>>>>>>>>>>>>>>>>" + flowid);
        BatchconferenceEntity entity = (BatchconferenceEntity) this.batchconferenceService.getBaseMapper().selectById(flowid);
        entity.setStata(ConferenceStateEnum.TO_BE_ATTENDED.getValue());
        this.batchconferenceService.updateById(entity);
        return R.ok();
    }

    @RequestMapping({"/update"})
    public R update(@RequestBody BatchconferenceEntity batchconference) {
        this.batchconferenceService.getBaseMapper().updateById(batchconference);
        return R.ok();
    }

    @PostMapping({"/delete"})
    @Transactional
    @ApiOperation("批次删除")
    public R delete(@RequestBody String[] flowids) {
        this.batchconferenceService.getBaseMapper().deleteBatchIds(Arrays.asList(flowids));
        for (String batid : flowids) {
            for (ConfernamelistEntity entity : this.confernamelistService.getBaseMapper().selectList(new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getBatchid, batid))) {
                this.confernamelistService.getBaseMapper().deleteById(entity.getFlowid());
                this.postchangeService.getBaseMapper().delete(new LambdaQueryWrapper<PostchangeEntity>().eq(PostchangeEntity::getNamelistid, entity.getFlowid()));
            }
        }
        return R.ok();
    }
}
